package com.financing.platform.service.impl;

 
 
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.jfree.util.Log;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.financing.platform.activities.ActivitiesBusiness;
import com.financing.platform.activities.ActivitiesBusinessObject;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dao.IActivitesDao;
import com.financing.platform.dto.ActivitesDTO;
import com.financing.platform.dto.ActivitesExpandDTO;
import com.financing.platform.service.IActivitesService;
 


@Service("activitesServiceImpl")
public class ActivitesServiceImpl implements IActivitesService,ApplicationContextAware{

	Logger logger =  Logger.getLogger(getClass());
 

	@Autowired
	private IActivitesDao activitesDao;
	
	private ApplicationContext applicationContext;
	

	@Override
	public ActivitesDTO searchActivitesByCode(String activitesCode) throws SQLException {
		 
		return activitesDao.searchActivitesByCode(activitesCode);
	}


	@Override
	public boolean isOutDateByCode(String code) {
		try {
		
			ActivitesDTO dto = activitesDao.searchActivitesByCode(code);
			Date startDate = dto.getActiviesStartTime();
			Date endDate = dto.getActiviesEndTime();
			Date currentDate = new Date();
			if(currentDate.before(startDate)||currentDate.after(endDate)){
				Log.info("Code out date now");
				return true;
			}
			
			//判断FA认证开始时间和结束时间
			startDate = dto.getActiviesFaAuthStartTime();
			endDate = dto.getActiviesFaAuthEndTime();
			if(null != startDate && null != endDate){
				if(currentDate.before(startDate)||currentDate.after(endDate)){
					Log.info("Code out date now");
					return true;
				}
			}
			
			if(null == dto.getActiviesTotalAmount() || dto.getActiviesTotalAmount() .compareTo(new BigDecimal(0)) <= 0){
				return false;
			}
			if(dto.getActiviesNowAmount().compareTo(dto.getActiviesTotalAmount()) >=0){
				return true;
			}
		} catch (SQLException e) {
			Log.warn("sql query for code out date error!",e);
			return true;
		}
		return false;
	}


	@Override
	public List<ActivitesExpandDTO> searchActivitesExpand(String activitesCode)
			throws SQLException {
		return activitesDao.searchActivitesExpand(activitesCode);
	}


	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public ResultMap ActivitiesJobExecution(String businessType,Object object) throws Exception {
		
		logger.info("ActivitiesJobExecution begin 注入活动开始!!!");
		logger.info("ActivitiesJobExecution begin businessType:"+businessType);
		
		List<ActivitesExpandDTO>  list =  searchActivitesExpand(null);
		for(ActivitesExpandDTO activitesExpandDTO : list){
			try {
				//判断活动过期
				if(isOutDateByCode(activitesExpandDTO.getActiviesCode())){
					continue;
				}
				//判断是否存在处理类型
				if(activitesExpandDTO.getBusinessType().indexOf(businessType)<0){
					continue;
				}
				logger.info("ActivitiesJobExecution begin ActiviesCode:"+activitesExpandDTO.getActiviesCode());
				logger.info("ActivitiesJobExecution begin BusinessName:"+activitesExpandDTO.getBusinessName());
				
				//获取spring beanid 实例
				ActivitiesBusiness activitiesBusiness =  (ActivitiesBusiness) applicationContext.getBean(activitesExpandDTO.getBusinessName());
				
				//根据类的放射执行 类的 activitesExpandDTO.getBusinessType() 方法。
				Class bean = activitiesBusiness.getClass();
				Method method = bean.getMethod(activitesExpandDTO.getBusinessType(),Object.class);
				method.invoke(activitiesBusiness,object);
			} catch (Exception e) {
				logger.error("ActivitiesJobExecution error", e);
				continue;
			}
		}
		logger.info("ActivitiesJobExecution end 注入活动结束!!!");
		return null;
	}


	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext  =  applicationContext;
		
	}


	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public ResultMap ActivitiesJobExecutionObject(String businessType,
			Object object) throws Exception {
		logger.info("ActivitiesJobExecutionObject begin 注入活动开始!!!");
		logger.info("ActivitiesJobExecutionObject begin businessType:"+businessType);
		
		List<ActivitesExpandDTO>  list =  searchActivitesExpand(null);
		for(ActivitesExpandDTO activitesExpandDTO : list){
			try {
				//判断活动过期
				if(isOutDateByCode(activitesExpandDTO.getActiviesCode())){
					continue;
				}
				//判断是否存在处理类型WIN_TYPE
				if(activitesExpandDTO.getBusinessType().indexOf(businessType)<0){
					continue;
				}
				logger.info("ActivitiesJobExecutionObject begin ActiviesCode:"+activitesExpandDTO.getActiviesCode());
				logger.info("ActivitiesJobExecutionObject begin BusinessName:"+activitesExpandDTO.getBusinessName());
				
				//获取spring beanid 实例
				ActivitiesBusinessObject activitiesBusinessObject =  (ActivitiesBusinessObject) applicationContext.getBean(activitesExpandDTO.getBusinessName());
				
				//根据类的放射执行 类的 activitesExpandDTO.getBusinessType() 方法。
				Class bean = activitiesBusinessObject.getClass();
				Method method = bean.getMethod(activitesExpandDTO.getBusinessType(),Object.class);
				method.invoke(activitiesBusinessObject,object);
			} catch (Exception e) {
				logger.error("ActivitiesJobExecutionObject error", e);
				continue;
			}
		}
		logger.info("ActivitiesJobExecutionObject end 注入活动结束!!!");
		return null;
	}


	@Override
	public boolean isOutDateByCode(String code, Date date) {
		try {
			
			ActivitesDTO dto = activitesDao.searchActivitesByCode(code);
			Date startDate = dto.getActiviesStartTime();
			Date endDate = dto.getActiviesEndTime();
			Date currentDate = date;
			if(currentDate.before(startDate)||currentDate.after(endDate)){
				Log.info("Code out date now");
				return true;
			}
		} catch (SQLException e) {
			Log.warn("sql query for code out date error!",e);
			return true;
		}
		return false;
	}
	
}
